package com.example.socialcontactapp.service.impl;

import com.aliyun.tea.utils.StringUtils;
import com.example.socialcontactapp.dao.ChatDao;
import com.example.socialcontactapp.entity.Chat;
import com.example.socialcontactapp.service.ChatService;
import com.example.socialcontactapp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2022/8/2
 **/
@Service("chatService")
public class ChatServiceImpl implements ChatService {
    @Resource
    private ChatDao chatDao;

    @Override
    public R getChats(Long sender, Long receiver, Long before, Long after) {
        if (sender == null || receiver == null) {
            return R.error().data("msg", "参数不对");
        }
        if (before != null && after != null) {
            List<Chat> chats1 = chatDao.queryBetween(sender, receiver, before, after);
            List<Chat> chats2 = chatDao.queryBetween(receiver, sender, before, after);
            return R.ok().data("list", merge(chats1, chats2));
        } else if (before != null && after == null) {
            List<Chat> chats1 = chatDao.queryBefore(sender, receiver, before);
            List<Chat> chats2 = chatDao.queryBefore(receiver, sender, before);
            return R.ok().data("list", merge(chats1, chats2));
        } else if (before == null && after != null) {
            List<Chat> chats1 = chatDao.queryAfter(sender, receiver, after);
            List<Chat> chats2 = chatDao.queryAfter(receiver, sender, after);
            return R.ok().data("list", merge(chats1, chats2));
        } else {
            List<Chat> chats1 = chatDao.query(sender, receiver);
            List<Chat> chats2 = chatDao.query(receiver, sender);
            return R.ok().data("list", merge(chats1, chats2));
        }
    }

    @Override
    public R sendChat(Long sender, Long receiver, String content) {
        if (sender == null || receiver == null) {
            return R.error().data("msg", "参数不对");
        }
        if (StringUtils.isEmpty(content)) {
            return R.error().data("msg", "发送内容不能为空");
        }
        Chat chat = new Chat();
        chat.setSender(sender);
        chat.setReceiver(receiver);
        chat.setContent(content);
        chat.setTimestamp(System.currentTimeMillis());

    }

    private List<Chat> merge(List<Chat>  chats1, List<Chat> chats2) {
        List<Chat> result = new LinkedList<>();
        int index1 = 0, index2 = 0;
        while (index1 < chats1.size() || index2 < chats2.size()) {
            if (index1 < chats1.size() && index2 < chats2.size()) {
                if (chats1.get(index1).getTimestamp() <= chats2.get(index2).getTimestamp()) {
                    result.add(chats1.get(index1));
                    index1++;
                } else {
                    result.add(chats2.get(index2));
                    index2++;
                }
            } else if (index1 < chats1.size() && index2 == chats2.size()) {
                result.add(chats1.get(index1));
                index1++;
            } else if (index1 == chats2.size() && index2 < chats2.size()) {
                result.add(chats2.get(index2));
                index2++;
            } else {
                break;
            }
        }
        return result;
    }
}
